package com.genesis.yunnanji.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.PavilionAdapter;
import com.genesis.yunnanji.bean.PavilionBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.GenesisUtils;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONObject;
import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;


public class Pavilion extends Fragment {

    private RecyclerView mRecyclerView;
    private int page = 1;
    private List<PavilionBean.ResultBean.ListBean> list;
    private View view;
    private PavilionAdapter adapter;
    private GenesisUtils genesisUtils;
    private Context context = getContext();

    public static Pavilion newInstance(String param) {
        Pavilion obj = new Pavilion();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.activity_pavilion, null);
            mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_pavilion_rv);
            genesisUtils = GenesisUtils.getIntance();
            initView();
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }

        return view;
    }

    protected void initView() {
        list = new ArrayList<>();
        getData(null);
    }


    private void getData(final TwinklingRefreshLayout refreshLayout) {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.PAVILION_LIST);
        params.addBodyParameter("p", 1 + "");
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Log.e("dizhoudata", jsonObject.toString());
                Gson gson = new Gson();
                PavilionBean pavilionBean = gson.fromJson(jsonObject.toString(), PavilionBean.class);
                if (pavilionBean.getStatus() == 200) {
                    for (int j = 0; j < pavilionBean.getResult().getList().size();
                         j++) {
                        list.add(pavilionBean.getResult().getList().get(j));
                    }
                    mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
                    mRecyclerView.setNestedScrollingEnabled(false);
                    adapter = new PavilionAdapter(getContext(), list);
                    mRecyclerView.setAdapter(adapter);
                    LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.x30));
                    LinearLayout lineLayout = new LinearLayout(getContext());
                    lineLayout.setOrientation(LinearLayout.VERTICAL);
                    lineLayout.setLayoutParams(params);
                    lineLayout.setBackgroundColor(getResources().getColor(R.color.divider));
                    adapter.addHeaderView(lineLayout);
                    mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(context).size((int) getResources().getDimension(R.dimen.x10)).color(Color.parseColor("#f5f5f5")).build());
                }
            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
                if (refreshLayout != null) {
                    refreshLayout.finishRefreshing();
                    refreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

}
